<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!--0:引入 vue.js-->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!--界面-->
<div id="app">
    <div style="position: fixed;width: 100px;height: 100px;background-color: brown;" :style = "myStyle"   >

    </div>

</div>
<script>
    var vm = new Vue({
        el:"#app",
        data:{
            myStyle:{
                top:"300px",
                left:"300px",
            }
        }
    });

    window.onkeydown = function (e) {
        //左: 37  上: 38  右: 39  下: 40
        if (e.keyCode == 39) {
          //右
          var left = parseInt(vm.myStyle.left);
          left += 20;
          vm.myStyle.left = left + "px";
        } else if (e.keyCode == 40) {
          //下
          var top = parseInt(vm.myStyle.top);
          top += 20;
          vm.myStyle.top = top + "px";
        } else if (e.keyCode == 38) {
          //上
          var top = parseInt(vm.myStyle.top);
          top -= 20;
          vm.myStyle.top = top + "px";
        } else if (e.keyCode == 37) {
          //左
          var left = parseInt(vm.myStyle.left);
          left -= 20;
          vm.myStyle.left = left + "px";
        }
      };
</script>
    
</body>
</html>